package com.yuncheng.oaApi.bxdService;

import cn.hutool.core.collection.CollectionUtil;
import com.yuncheng.oaApi.constant.OaCommonFlowConstant;
import com.yuncheng.oaApi.constant.OaJcyFybxdConstant;
import com.yuncheng.oaApi.constant.OaSpcyJcyFybxdConstant;
import com.yuncheng.oaApi.entity.*;
import com.yuncheng.oaApi.mapper.OaSpcyJcyFybxdtssxsqMapper;
import com.yuncheng.spcyApi.utils.DateConvert;
import com.yuncheng.oaApi.utils.GetUuIdUtils;
import com.yuncheng.oaApi.utils.NumberToChineseCapital;
import com.yuncheng.oaApi.vo.*;
import com.yuncheng.spcyApi.constant.common.LogConstant;
import com.yuncheng.spcyApi.constant.common.SpcyConstant;
import com.yuncheng.spcyApi.entity.SpcySqsxWjjl;
import com.yuncheng.spcyApi.service.ISpcyLogService;
import com.yuncheng.spcyApi.service.ISpcySqsxWjjlService;
import com.yuncheng.spcyApi.utils.ChineseMoneyUtils;
import com.yuncheng.spcyApi.utils.GetCurrentUserUtils;
import com.yuncheng.spcyApi.utils.StringPatternUtils;
import org.apache.commons.collections.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuncheng.constant.*;
import com.yuncheng.entity.*;
import com.yuncheng.exception.BusinessException;
import com.yuncheng.oaApi.flow.OaSpcyFlowConstant;
import com.yuncheng.oaApi.mapper.OaSpcyJcyFybxdMapper;
import com.yuncheng.oaApi.service.*;
import com.yuncheng.vo.*;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import utils.DateConversionUtils;
import utils.LogUtils;
import utils.ObjectToMapUtils;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.ParseException;
import java.util.*;

/**
 * @author hua
 */
@Component(value = "oaSpcyJcyFybxdService22")
public class OaSpcyJcyFybxdService {

    private static final Logger log = LoggerFactory.getLogger(OaJcyFybxdService.class);

    @Resource
    private IOaSpcyJcyFybxdgrjlService oaSpcyJcyFybxdgrjlService;

    @Resource
    private IOaSpcyJcyFybxdService oaSpcyJcyFybxdService;

    @Resource
    private OaSpcyJcyFybxdMapper oaSpcyJcyFybxdMapper;

    @Resource
    private IOaSpcyJcyFybxxmmxjlService oaSpcyJcyFybxxmmxjlService;

    @Resource
    private IOaSpcyJcyFybxdtssxsqService OaSpcyJcyFybxdtssxsqService;

    @Resource
    private ISpcySqsxWjjlService spcySqsxWjjlService;

    @Resource
    private IOaYcSysUserService oaSysUserService;

    @Resource
    private ISpcyLogService spcyLogService;

    @Resource
    private IOaNkRsDjhglService oaNkRsDjhglService;

    @Resource
    @Lazy
    private INkBxJtxcdService nkBxJtxcdService;

    @Resource
    @Lazy
    private INkRsZzsqdService nkRsZzzsqdService;

    @Resource
    @Lazy
    private OaSpcyJcyFybxdtssxsqMapper oaSpcyJcyFybxdtssxsqMapper;


    /**
     * 初始化费用报销单-新增人员个人记录
     * @param user 用户
     * @param fybxd 报销单
     * @param fpjsj 派检时间
     * @param fjcsf 检查身份(组长/组员/预备检查员)
     */
    private void initFybxdGrjl(YcSysUser user, OaSpcyJcyFybxd fybxd, Date fpjsj, String fjcsf){
        List<OaSpcyJcyFybxdgrjl> mxList = oaSpcyJcyFybxdgrjlService.list(new LambdaQueryWrapper<OaSpcyJcyFybxdgrjl>()
                .eq(OaSpcyJcyFybxdgrjl::getFbxdid, fybxd.getId())
                .eq(OaSpcyJcyFybxdgrjl::getFuserid, user.getId()));
        if (CollectionUtil.isNotEmpty(mxList)){
            return;
        }

        OaSpcyJcyFybxdgrjl bxdGrjl = new OaSpcyJcyFybxdgrjl();
        bxdGrjl.setId(GetUuIdUtils.ReplaceUuId());
        bxdGrjl.setFsqsxid(fybxd.getFsqsxid());
        bxdGrjl.setFbxdid(fybxd.getId());
        bxdGrjl.setFuserid(user.getId());

        bxdGrjl.setRealname(user.getRealname());
        bxdGrjl.setSex(user.getSex());
        bxdGrjl.setFsfzh(user.getFsfzh());
        bxdGrjl.setPhone(user.getPhone());
        bxdGrjl.setFgzdw(user.getFgzdw());

        // 银行卡
        bxdGrjl.setFkfh(user.getFkfh());
        bxdGrjl.setFyhkh(user.getFyhkh());
        bxdGrjl.setFkhhszd(user.getFkhhszd());

        // 公务卡
        bxdGrjl.setFgwkh(user.getFgwkh());
        bxdGrjl.setFgwkkhh(user.getFgwkkhh());
        bxdGrjl.setFgwkszd(user.getFgwkszd());

        bxdGrjl.setFpjks(fybxd.getFjbks());
        bxdGrjl.setFpjksid(fybxd.getFjbksid());
        bxdGrjl.setFpjr(fybxd.getFjbr());
        bxdGrjl.setFpjrid(fybxd.getFjbrid());
        //有无劳务费
        bxdGrjl.setFywlwf("有");
        bxdGrjl.setFpjsj(fpjsj);
        if (StringUtils.isNotBlank(fjcsf)) {
            bxdGrjl.setFjcsf(fjcsf);
        }else {
            bxdGrjl.setFjcsf("无");
        }

        /**
         * 劳务费实报金额
         * 组长350/天
         * 组员300/天
         * 预备检查员 0/天
         * 观察员 0/天
         * */
        int hsbzts = 0;  // 伙食补助天数 默认按检查天数+1，观察员0天后面金额也为0
        int snjtbzts = 0; // 市内交通补助天数 默认按检查天数+1，观察员0天后面金额也为0

        int lwfts = 0; // 劳务费天数 默认按检查天数，观察员0天后面金额也为0
        int zxfts = 0; // // 住宿费天数  默认按检查天数

        String lwfsbje = "0";
        String zxfybje = "0";
        if (StringUtils.isNotBlank(bxdGrjl.getFjcsf())) {
            if (bxdGrjl.getFjcsf().contains(SpcyConstant.HCY_SSK_ZZ)) {
                lwfts = fybxd.getFjcts();
                lwfsbje = String.valueOf(fybxd.getFjcts() * 350);

                hsbzts = fybxd.getFjcts() + 1;
                snjtbzts = fybxd.getFjcts() + 1;

            } else if (bxdGrjl.getFjcsf().contains(SpcyConstant.HCY_SSK_ZY)) {
                lwfts = fybxd.getFjcts();
                lwfsbje = String.valueOf(fybxd.getFjcts() * 300);

                hsbzts = fybxd.getFjcts() + 1;
                snjtbzts = fybxd.getFjcts() + 1;
            }
        }
        zxfts = lwfts + 1;
//        zxfybje = String.valueOf(zxfts * 350);

        bxdGrjl.setFlwfts(lwfts);
        bxdGrjl.setFlwfsbje(lwfsbje);

        bxdGrjl.setFhsbzts(hsbzts); // 伙食补助天数 默认按检查天数+1，观察员0天后面金额也为哦0
        bxdGrjl.setFhsbzsbje(String.valueOf(bxdGrjl.getFhsbzts()*100)); // 伙食补助天数 检查员100/天 观察员0/天

        bxdGrjl.setFsnjtbzts(snjtbzts); // 市内交通补助天数 默认按检查天数+1，观察员0天后面金额也为0
        bxdGrjl.setFsnjtbzje(String.valueOf(bxdGrjl.getFhsbzts()*80));  // 市内交通补助金额 检查员80/天 观察员0/天

        bxdGrjl.setFcsjjtfs(OaSpcyJcyFybxdConstant.CSJJTFS_DEFAULT_GGJT); // 城市间交通方式  单位派车/无单位派车/公共交通 （默认公共交通，如果选其他不需要填后面的城市间交通费字段）

        bxdGrjl.setFwjtfsfdf(OaSpcyJcyFybxdConstant.FYBXD_SFDF_NO); // 城市间交通费-往-是否垫付 否/是（默认否，选是必填垫付人）
        bxdGrjl.setFwjtfdfrid(""); // 城市间交通费-往-垫付人id 从本次审评查验费用报销个人记录选择人员id
        bxdGrjl.setFwjtfqscs(""); // 城市间交通费-往-起始城市 可选全国的城市，前面14个显示广西地市
        bxdGrjl.setFwjtfddcs(""); // 城市间交通费-往-到达城市 可选全国的城市，前面14个显示广西地市
        bxdGrjl.setFwjtfsbje("0"); // 城市间交通费-往-实报金额 如果本人选择是垫付 实报金额为0，把本人的合计金额加到垫付人的实报金额
        bxdGrjl.setFwjtfybje("0"); // 城市间交通费-往-应报金额 手填

        bxdGrjl.setFfjtfsfdf(OaSpcyJcyFybxdConstant.FYBXD_SFDF_NO); // 城市间交通费-返-是否垫付 否/是（默认否，选是必填垫付人）
        bxdGrjl.setFfjtfdfrid(""); // 城市间交通费-返-垫付人id 从本次审评查验费用报销个人记录选择人员id
        bxdGrjl.setFfjtfqscs(""); // 城市间交通费-返-起始城市 可选全国的城市，前面14个显示广西地市
        bxdGrjl.setFfjtfddcs(""); // 城市间交通费-返-到达城市 可选全国的城市，前面14个显示广西地市
        bxdGrjl.setFfjtfybje("0"); // 城市间交通费-返-应报金额 手填
        bxdGrjl.setFfjtfsbje("0"); // 城市间交通费-返-实报金额 如果本人选择是垫付 实报金额为0，把本人的合计金额加到垫付人的实报金额

        bxdGrjl.setFzxfts(zxfts); // 住宿费天数  默认按检查天数
        bxdGrjl.setFzxfsfdf(OaSpcyJcyFybxdConstant.FYBXD_SFDF_NO); // 住宿费是否垫付 否/是（默认否，选是必填垫付人）
        bxdGrjl.setFzxfdfrid(""); // 住宿费垫付人id 从本次审评查验费用报销个人记录选择人员id
        bxdGrjl.setFzxfybje(zxfybje); // 住宿费应报金额 手填
        bxdGrjl.setFzxfsbje(zxfybje); // 住宿费实报金额 如果本人选择是垫付 实报金额为0，把本人的合计金额加到垫付人的实报金额

        /**
         * 差旅费应报合计金额 = 伙食补助实报金额+城市间交通费-往-合计金额+城市间交通费-返-合计金额+住宿费合计金额
         * 差旅费实报合计金额 = 伙食补助实报金额+城市间交通费-往-实报金额+城市间交通费-返-实报金额+住宿费实报金额
         */
        bxdGrjl.setFclfybhjje("0");
        bxdGrjl.setFclfsbhjje("0");


        bxdGrjl.setFlwfbz("300");
        bxdGrjl.setFhsbzbz("100");
        bxdGrjl.setFsnjtbzbz("80");
        oaSpcyJcyFybxdgrjlService.save(bxdGrjl);
    }

    /**
     * 删除费用报销单及明细
     * @param fbxdid
     */
    public void deleteFybxdAndRymx(String fbxdid){
        if (StringUtils.isBlank(fbxdid)){
            throw new BusinessException("参数为空！");
        }
        OaSpcyJcyFybxd bean = oaSpcyJcyFybxdService.getById(fbxdid);
        if (bean == null){
            throw new BusinessException("没有相匹配的数据！");
        }

        oaSpcyJcyFybxdService.removeById(fbxdid);
        oaSpcyJcyFybxdgrjlService.remove(new LambdaQueryWrapper<OaSpcyJcyFybxdgrjl>()
                .eq(OaSpcyJcyFybxdgrjl::getFbxdid, fbxdid));

        String log = LogUtils.MapToStringMethod(ObjectToMapUtils.objectToMap(bean));
        spcyLogService.addLogs(LogConstant.LOGS_JCY_FYBXD, log, fbxdid, "删除检查员费用报销单及明细",SpcyConstant.LOGS_SYS_TYPE_SC);
    }

    /**
     * 查询费用报销单-分页列表
     */
    public HttpResult queryFybxdPageList(OaSpcyJcyFybxd fybxd, String gjz, boolean isSelectMy, PageVo pageVo, OrderItemVo orderItemVo){
        Page<OaSpcyJcyFybxd> page = new Page<>(pageVo.getPageNo(), pageVo.getPageSize());

        if (isSelectMy){
            Page<OaSpcyJcyFybxd> pageList = oaSpcyJcyFybxdMapper.selectFybxdPageList(page, gjz, GetCurrentUserUtils.getCurrentUserId(), fybxd, orderItemVo);

            return HttpResult.ok(pageList);
        }else {
            LambdaQueryWrapper<OaSpcyJcyFybxd> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.like(StringUtils.isNotBlank(fybxd.getFjcsxmc()), OaSpcyJcyFybxd::getFjcsxmc, fybxd.getFjcsxmc());

            if (StringUtils.isNotBlank(gjz)) {
                queryWrapper
                        .and(f -> f.like(OaSpcyJcyFybxd::getFbxnrms, gjz)
                                .or()
                                .like(OaSpcyJcyFybxd::getFjbr, gjz)
                                .or()
                                .like(OaSpcyJcyFybxd::getFjbks, gjz)
                                .or()
                                .like(OaSpcyJcyFybxd::getFjcsxmc, gjz)
                                .or()
                                .like(OaSpcyJcyFybxd::getFjcqymc, gjz)
                                .or()
                                .like(OaSpcyJcyFybxd::getFqydz, gjz)
                        );
            }
            Page<OaSpcyJcyFybxd> pageList = oaSpcyJcyFybxdService.page(page, queryWrapper);

            return HttpResult.ok(pageList);
        }
    }

    /**
     * 查询报销单列表-根据sqsxid
     */
    public List<OaSpcyJcyFybxd> queryBxdListBySqsxId(String sqsxid){
        if (StringUtils.isBlank(sqsxid)){
            throw new BusinessException("申请事项参数为空！");
        }

        List<OaSpcyJcyFybxd> bxdList = oaSpcyJcyFybxdService.list(new LambdaQueryWrapper<OaSpcyJcyFybxd>()
                .eq(OaSpcyJcyFybxd::getFsqsxid, sqsxid));
        if (CollectionUtil.isEmpty(bxdList)) {

        }

        return oaSpcyJcyFybxdService.list(new LambdaQueryWrapper<OaSpcyJcyFybxd>()
                .eq(OaSpcyJcyFybxd::getFsqsxid, sqsxid)
                .orderByAsc(OaSpcyJcyFybxd::getCreateTime));
    }

    /**
     * 查询报销人员信息列表-根据sqsxid
     * @param sqsxid
     */
    public Map queryBxryxxListBySqsxId(String sqsxid,String bxdid){
        if (StringUtils.isNotBlank(sqsxid)){
            OaSpcyJcyFybxd fybxd = null;
            List<OaSpcyJcyFybxd> bxdList = oaSpcyJcyFybxdService.list(new LambdaQueryWrapper<OaSpcyJcyFybxd>()
                    .eq(OaSpcyJcyFybxd::getFsqsxid, sqsxid));
            if (CollectionUtil.isEmpty(bxdList)) {


                List<OaSpcyJcyFybxd> bxdList2 = oaSpcyJcyFybxdService.list(new LambdaQueryWrapper<OaSpcyJcyFybxd>()
                        .eq(OaSpcyJcyFybxd::getFsqsxid, sqsxid));
                fybxd = bxdList2.get(0);
            }else {
                fybxd = bxdList.get(0);
            }

            Map resultMap = new HashMap();

            resultMap.put("bxd", fybxd);
            resultMap.put("bxmxList", this.queryBxryjlBySqsxId(sqsxid));

            return resultMap;
        }else if (StringUtils.isNotBlank(bxdid)){
            OaSpcyJcyFybxd fybxd = null;
            List<OaSpcyJcyFybxd> bxdList = oaSpcyJcyFybxdService.list(new LambdaQueryWrapper<OaSpcyJcyFybxd>()
                    .eq(OaSpcyJcyFybxd::getId, bxdid));
            if (CollectionUtil.isEmpty(bxdList)) {

                List<OaSpcyJcyFybxd> bxdList2 = oaSpcyJcyFybxdService.list(new LambdaQueryWrapper<OaSpcyJcyFybxd>()
                        .eq(OaSpcyJcyFybxd::getId, bxdid));
                fybxd = bxdList2.get(0);
            }else {
                fybxd = bxdList.get(0);
            }

            Map resultMap = new HashMap();

            resultMap.put("bxd", fybxd);
            resultMap.put("bxmxList", this.queryBxryjlBySqsxId(bxdid));

            return resultMap;

        }else {
            throw new BusinessException("参数为空！");
        }

    }

    /**
     * 查询报销人员信息列表-根据申请事项id
     * @param sqsxid
     */
    public List<OaSpcyJcyFybxdgrjl> queryBxryjlBySqsxId(String sqsxid){
        return oaSpcyJcyFybxdgrjlService.list(new LambdaQueryWrapper<OaSpcyJcyFybxdgrjl>()
                .eq(OaSpcyJcyFybxdgrjl::getFsqsxid, sqsxid)
                .last("order by instr ('组长,组员,预备检查员,观察员', fjcsf)"));
    }

    /**
     * 查询报销人员信息列表-根据报销单id
     * @param bxdid
     */
    public List<OaSpcyJcyFybxdgrjl> queryBxryxxListByBxdId(String bxdid){
        return oaSpcyJcyFybxdgrjlService.list(new LambdaQueryWrapper<OaSpcyJcyFybxdgrjl>()
                .eq(OaSpcyJcyFybxdgrjl::getFbxdid, bxdid)
                .last("order by instr ('组长,组员,预备检查员,观察员', fjcsf), create_time asc"));
    }

    /**
     * 查询专家劳务费列表-根据报销单id
     * @param bxdid
     */
    public List<OaSpcyJcyFybxdgrjl> queryZjlwfList(String bxdid){
        List<OaSpcyJcyFybxdgrjl> list = oaSpcyJcyFybxdgrjlService.list(new LambdaQueryWrapper<OaSpcyJcyFybxdgrjl>()
                .eq(OaSpcyJcyFybxdgrjl::getFbxdid, bxdid)
                .eq(OaSpcyJcyFybxdgrjl::getFlwfsfjgs, OaSpcyJcyFybxdConstant.FYBXD_LWFSFJGS_YES)
                .orderByAsc(OaSpcyJcyFybxdgrjl::getCreateTime));

        return list;
    }

    /**
     * 查询当前记录的对应垫付人员列表
     * @param fbxdid 报销单id
     * @param id 报销人员记录id
     */
    public List<Map> queryFybxdDfryList(String fbxdid, String id){

        List<Map> resultList = new ArrayList<>();

        List<OaSpcyJcyFybxdgrjl> list = oaSpcyJcyFybxdgrjlService.list(new LambdaQueryWrapper<OaSpcyJcyFybxdgrjl>()
                .eq(OaSpcyJcyFybxdgrjl::getFbxdid, fbxdid)
                .ne(StringUtils.isNotBlank(id), OaSpcyJcyFybxdgrjl::getFuserid, id));
        if (CollectionUtil.isNotEmpty(list)) {
            for (OaSpcyJcyFybxdgrjl b : list) {
                Map dataMap = new HashMap();
                dataMap.put("id", b.getFuserid());
                dataMap.put("realname", b.getRealname());
                dataMap.put("fjcsf", b.getFjcsf());
                dataMap.put("sex", b.getSex());
                dataMap.put("fgzdw", b.getFgzdw());
                dataMap.put("phone", b.getPhone());
                resultList.add(dataMap);
            }
        }
        return resultList;
    }

    /**
     * 新增报销人员用户信息
     */
    public HttpResult addBxryUserInfo(AddJcyBxryInfoVo bean) throws InvocationTargetException, IllegalAccessException {
        /*if (StringUtils.isBlank(bean.getFbxdid())){
            throw new BusinessException("参数为空!");
        }*/

        OaSpcyJcyFybxd fybxd = null;
        if (StringUtils.isBlank(bean.getFbxdid())){
            if (StringUtils.isBlank(bean.getSqsxid())){
                return HttpResult.error("操作失败，事项参数为空！");
            }

            List<OaSpcyJcyFybxd> list = oaSpcyJcyFybxdService.list(new LambdaQueryWrapper<OaSpcyJcyFybxd>().eq(OaSpcyJcyFybxd::getFsqsxid, bean.getSqsxid()));
            if (CollectionUtil.isEmpty(list)){
                return HttpResult.error("操作失败，没有相匹配的报销数据！");
            }
            fybxd = list.get(0);

        }else {
            fybxd = oaSpcyJcyFybxdService.getById(bean.getFbxdid());
            if (fybxd == null){
                return HttpResult.error("操作失败，没有相匹配的报销数据！");
            }
        }


        if (StringUtils.isBlank(bean.getRealname())){
            return HttpResult.error("操作失败，姓名为空！");
        }

        if (StringUtils.isBlank(bean.getFsfzh())){
            return HttpResult.error("操作失败，身份证号为空！");
        }

        if (StringUtils.isBlank(bean.getPhone())){
            return HttpResult.error("操作失败，手机号为空！");
        }

        if (StringUtils.isBlank(bean.getFgzdw())){
            return HttpResult.error("操作失败，工作单位为空！");
        }

        String realname = StringPatternUtils.trimString(bean.getRealname());
        String fsfzh = StringPatternUtils.trimString(bean.getFsfzh());
        String phone = StringPatternUtils.trimString(bean.getPhone());

        // 验证身份证号
        List<YcSysUser> sfzhUserList = oaSysUserService.selectUserBySfzh(fsfzh);
        if (CollectionUtil.isNotEmpty(sfzhUserList)){
            return HttpResult.error("操作失败，身份证号已经存在！");
        }

        // 验证手机号
        List<YcSysUser> phoneUserList = oaSysUserService.selectUserBySfzh(fsfzh);
        if (CollectionUtil.isNotEmpty(phoneUserList)){
            return HttpResult.error("操作失败，手机号已经存在！");
        }

        String id = GetUuIdUtils.ReplaceUuId();
        YcSysUser user = new YcSysUser();
        user.setId(id);
        BeanUtils.copyProperties(user, bean);
        user.setRealname(realname);
        user.setFsfzh(fsfzh);
        user.setPhone(phone);

        // 解析身份证号获取出生年月
        try {
            Date birthDayFromIdCard = DateConversionUtils.getBirthDayFromIdCard(fsfzh);
            if (birthDayFromIdCard != null){
                user.setBirthday(birthDayFromIdCard);
            }
        } catch (ParseException e) {
            log.error("身份证解析失败！");
        }

        // 获取性别
        int sex = DateConversionUtils.getSexFromIdCard(fsfzh);
        user.setSex(sex);

        user.setCreateTime(new Date());
        user.setDelFlag(SysUserConstant.DEL_FLAG_YES);
        user.setStatus(SysUserConstant.STATUS_YES);
        oaSysUserService.save(user);

        initFybxdGrjl(user, fybxd, null, bean.getFjcsf());

        String operContent = "删除：";
        operContent += LogUtils.MapToStringMethod(ObjectToMapUtils.objectToMap(user));

        spcyLogService.addLogs(LogConstant.LOGS_JCY_FYBXD,operContent,bean.getSqsxid(),"新增费用报销单人员用户信息", SpcyConstant.LOGS_SYS_TYPE_XZ);

        return HttpResultVo.HttpResultSave(user,"添加成功");
    }

    /**
     * 查询可选择人员列表
     */
    public HttpResult queryKSelectUserPageList(YcSysUser query,String gjz, PageVo pageVo){
        return HttpResult.ok(oaSysUserService.getFybxdUserPageList(query,gjz,pageVo));
    }

    /**
     * 保存选择报销单-报销人员
     */
    public HttpResult saveSelectBxry(String ids, String fbxdid, String sqsxid, String fjcsf){
        if (StringUtils.isBlank(ids)){
            return HttpResult.error("请选择人员为空！");
        }

        OaSpcyJcyFybxd fybxd = null;

        if (StringUtils.isNotBlank(fbxdid)) {
            fybxd = oaSpcyJcyFybxdService.getById(fbxdid);

            if (fybxd == null) {
                return HttpResult.error("没有相匹配的报销数据！");
            }
        }else {
            if (StringUtils.isBlank(sqsxid)){
                return HttpResult.error("事项参数为空！");
            }

            if (StringUtils.isNotBlank(sqsxid)) {
                List<OaSpcyJcyFybxd> list = oaSpcyJcyFybxdService.list(new LambdaQueryWrapper<OaSpcyJcyFybxd>()
                        .eq(OaSpcyJcyFybxd::getFsqsxid, sqsxid));
                if (CollectionUtil.isEmpty(list)) {
                    return HttpResult.error("没有相匹配的报销数据！");
                }else {
                    fybxd = list.get(0);
                }
            }
        }

        List<YcSysUser> userList = oaSysUserService.listByIds(StringPatternUtils.getListByIds(ids));
        if (CollectionUtil.isEmpty(userList)){
            return HttpResult.error("没有相匹配的用户数据！");
        }
        for (YcSysUser user : userList) {
            initFybxdGrjl(user, fybxd, null, fjcsf);
        }

        return HttpResult.ok("操作成功");
    }

    /**
     * 更新报销人员明细
     */
    public void updateBxd(HashMap<String,String> buffer) throws IllegalAccessException, InvocationTargetException {
        String id = buffer.get("id");
        if (StringUtils.isBlank(id)){
            throw new BusinessException("参数为空！");
        }

        OaSpcyJcyFybxd updateBean = oaSpcyJcyFybxdService.getById(id);
        if (updateBean == null){
            throw new BusinessException("没有相匹配数据！");
        }

        Map<String, Object> oldMap = ObjectToMapUtils.objectToMap(updateBean);

        ConvertUtils.register(new DateConvert(), Date.class);
        // 自动完成页面到bean的数据对接
        BeanUtils.populate(updateBean, buffer);

        updateBean.setUpdateTime(new Date());
        updateBean.setUpdateBy(GetCurrentUserUtils.getCurrentUserAccount());
        oaSpcyJcyFybxdService.updateById(updateBean);

        String operContent = "基本信息修改：";
        operContent += LogUtils.BeanModifyMsg(updateBean,oldMap);

        spcyLogService.addLogs(LogConstant.LOGS_JCY_FYBXD,operContent,updateBean.getFsqsxid(),"修改费用报销单", SpcyConstant.LOGS_SYS_TYPE_XG);
    }

    /**
     * 更新报销人员明细
     */
    public void updateBxrymxxx(HashMap<String,String> buffer) throws IllegalAccessException, InvocationTargetException {
        String id = buffer.get("id");
        if (StringUtils.isBlank(id)){
            throw new BusinessException("参数为空！");
        }

        OaSpcyJcyFybxdgrjl updateBean = oaSpcyJcyFybxdgrjlService.getById(id);
        if (updateBean == null){
            throw new BusinessException("没有相匹配数据！");
        }
        OaSpcyJcyFybxdgrjl oldBean = updateBean;
//        Map<String, Object> oldMap = ObjectToMapUtils.objectToMap(updateBean);

        ConvertUtils.register(new DateConvert(), Date.class);
        // 自动完成页面到bean的数据对接
        BeanUtils.populate(updateBean, buffer);

        // 不为公共交通，相关字段全部置空
        if (StringUtils.isNotBlank(updateBean.getFcsjjtfs())
                && updateBean.getFcsjjtfs().equals(OaSpcyJcyFybxdConstant.CSJJTFS_DEFAULT_DWPC)){
            updateBean.setFwjtfsfdf(OaSpcyJcyFybxdConstant.FYBXD_SFDF_NO);
            updateBean.setFwjtfsbje("");
            updateBean.setFwjtfybje("");

            updateBean.setFwjtfdfrid("");
            updateBean.setFwjtfdfrxm("");
            updateBean.setFwjtfqscs("");
            updateBean.setFwjtfddcs("");

            updateBean.setFfjtfsfdf(OaSpcyJcyFybxdConstant.FYBXD_SFDF_NO);
            updateBean.setFfjtfsbje("");
            updateBean.setFfjtfybje("");

            updateBean.setFfjtfdfrid("");
            updateBean.setFfjtfdfrxm("");
            updateBean.setFfjtfqscs("");
            updateBean.setFfjtfddcs("");
        }

        // 劳务费
        int lwfts = Optional.ofNullable(updateBean.getFlwfts()).orElseGet(()->0);
        // 伙食补助
        int hsbzts = Optional.ofNullable(updateBean.getFhsbzts()).orElseGet(()->0);
        // 市内交通补助
        int snjtbzts = Optional.ofNullable(updateBean.getFsnjtbzts()).orElseGet(()->0);

        /** 计算劳务费、伙食补助、市内交通补助金额 */
        /**
         * 自动更新劳务费 劳务费、伙食补助、市内交通补助金额
         */
        updateBean = jsBxdmxLwfHsfJtbz(updateBean, lwfts, hsbzts, snjtbzts);

        if (StringUtils.isNotBlank(updateBean.getFywlwf()) && updateBean.getFywlwf().equals("无")){
            updateBean.setFlwfsbje("0");

        }

        if (StringUtils.isNotBlank(updateBean.getFfjtfsfdf()) && updateBean.getFfjtfsfdf().equals("否")){
            updateBean.setFfjtfdfrid("");
            updateBean.setFfjtfdfrxm("");
        }

        if (StringUtils.isNotBlank(updateBean.getFwjtfsfdf()) && updateBean.getFwjtfsfdf().equals("否")){
            updateBean.setFwjtfdfrid("");
            updateBean.setFwjtfdfrxm("");
        }

        if (StringUtils.isNotBlank(updateBean.getFzxfsfdf()) && updateBean.getFzxfsfdf().equals("否")){
            updateBean.setFzxfdfrid("");
            updateBean.setFzxfdfrxm("");
        }



        if (StringUtils.isNotBlank(updateBean.getFfjtfdfrid())) {
            updateBean.setFfjtfdfrxm(Optional.ofNullable(oaSysUserService.findRealnameById(updateBean.getFfjtfdfrid())).orElseGet(()->""));
        }
        if (StringUtils.isNotBlank(updateBean.getFwjtfdfrid())) {
            updateBean.setFwjtfdfrxm(Optional.ofNullable(oaSysUserService.findRealnameById(updateBean.getFwjtfdfrid())).orElseGet(()->""));
        }
        if (StringUtils.isNotBlank(updateBean.getFzxfdfrid())) {
            updateBean.setFzxfdfrxm(Optional.ofNullable(oaSysUserService.findRealnameById(updateBean.getFzxfdfrid())).orElseGet(()->""));
        }

        updateBean.setUpdateTime(new Date());
        updateBean.setUpdateBy(GetCurrentUserUtils.getCurrentUserAccount());
        boolean flag = oaSpcyJcyFybxdgrjlService.updateById(updateBean);
        if (flag){
            /** 重新计算汇总金额 */
            this.againHzBxd(updateBean.getFbxdid());
        }

        // 同步用户银行卡及公务卡信息
        oaSysUserService.updateSysYhkOrGwk(oldBean, updateBean);

//        String operContent = "基本信息修改：";
//        operContent += LogUtils.BeanModifyMsg(updateBean,oldMap);

//        spcyLogService.addLogs(LogConstant.LOGS_JCY_FYBXD,operContent,updateBean.getFsqsxid(),"修改费用报销单明细", SpcyConstant.LOGS_SYS_TYPE_XG);
    }

    /** 同步报销人员-银行卡或者公务卡 */
    private void syncUserYkhOrGwk(OaSpcyJcyFybxdgrjl grjl){
        if (grjl == null){
            return;
        }
        if (StringUtils.isBlank(grjl.getFuserid())){
            return;
        }
        boolean flag = false;
        LambdaUpdateWrapper<YcSysUser> updateWrapper = new LambdaUpdateWrapper();
        updateWrapper.eq(YcSysUser::getId, grjl.getFuserid());

        YcSysUser updateUser = new YcSysUser();

        // 银行卡
        if (StringUtils.isNotBlank(grjl.getFyhkh())){
            updateUser.setFyhkh(grjl.getFyhkh());
            flag = true;
        }
        if (StringUtils.isNotBlank(grjl.getFkfh())){
            updateUser.setFkfh(grjl.getFkfh());
            flag = true;
        }
        if (StringUtils.isNotBlank(grjl.getFkhhszd())){
            updateUser.setFkhhszd(grjl.getFkhhszd());
            flag = true;
        }

        // 公务卡
        if (StringUtils.isNotBlank(grjl.getFgwkh())){
            updateUser.setFgwkh(grjl.getFgwkh());
            flag = true;
        }
        if (StringUtils.isNotBlank(grjl.getFgwkkhh())){
            updateUser.setFgwkkhh(grjl.getFgwkkhh());
            flag = true;
        }
        if (StringUtils.isNotBlank(grjl.getFgwkszd())){
            updateUser.setFgwkszd(grjl.getFgwkszd());
            flag = true;
        }

        if (flag) {
            oaSysUserService.update(updateUser, updateWrapper);
        }

    }

    /**
     * 删除报销人员明细-根据报销人员id
     */
    public void deleteBxrymxById(String fybxdgrjlId){
        if (StringUtils.isBlank(fybxdgrjlId)){
            throw new BusinessException("参数为空！");
        }

        OaSpcyJcyFybxdgrjl updateBean = oaSpcyJcyFybxdgrjlService.getById(fybxdgrjlId);
        if (updateBean == null){
            throw new BusinessException("没有相匹配数据！");
        }

        oaSpcyJcyFybxdgrjlService.removeById(fybxdgrjlId);

        String operContent = "删除：";
        operContent += LogUtils.MapToStringMethod(ObjectToMapUtils.objectToMap(updateBean));

        spcyLogService.addLogs(LogConstant.LOGS_JCY_FYBXD,operContent,updateBean.getFsqsxid(),"删除费用报销单", SpcyConstant.LOGS_SYS_TYPE_SC);

    }

    /**
     * 报销单信息-查询报销单
     */
    public OaSpcyJcyFybxd queryBxdById(String fbxdid){
        return oaSpcyJcyFybxdService.getById(fbxdid);
    }

    /**
     * 报销单信息-提交
     */
    public HttpResult submitFybxd(String fbxdid){
        if (StringUtils.isBlank(fbxdid)){
            return HttpResult.error("参数为空！");
        }
        OaSpcyJcyFybxd bean = oaSpcyJcyFybxdService.getById(fbxdid);
        if (bean == null){
            return HttpResult.error("没有相匹配的数据！");
        }

        String nextState = OaSpcyJcyFybxdConstant.getNextState(bean.getFbxdzt());

        if (nextState.equals(OaSpcyJcyFybxdConstant.STATE_TSSXSQ)){
//            verifyIsExistsFybxdBxryInUserQm(fbxdid); // 验证签名
        }
        else if (nextState.equals(OaSpcyJcyFybxdConstant.STATE_LWF)){

            List<OaSpcyJcyFybxdtssxsq> tssxsqList = OaSpcyJcyFybxdtssxsqService.list(new LambdaQueryWrapper<OaSpcyJcyFybxdtssxsq>()
                    .eq(OaSpcyJcyFybxdtssxsq::getFbxdid, fbxdid));
            if (CollectionUtil.isNotEmpty(tssxsqList)){
                bean.setFsfytssxsq(OaSpcyJcyFybxdConstant.FYBXD_SFYTSSXSQ_YES);
            }else {
                bean.setFsfytssxsq(OaSpcyJcyFybxdConstant.FYBXD_SFYTSSXSQ_NO);
            }

        } else if (nextState.equals(OaSpcyJcyFybxdConstant.STATE_JTF)) {
            //检查劳务费
            List<OaSpcyJcyFybxdgrjl> jcyFybxdgrjls = oaSpcyJcyFybxdgrjlService.list(new LambdaQueryWrapper<OaSpcyJcyFybxdgrjl>()
                    .eq(OaSpcyJcyFybxdgrjl::getFbxdid, fbxdid));
            if (CollectionUtil.isNotEmpty(jcyFybxdgrjls)){
                for (OaSpcyJcyFybxdgrjl jcyFybxdgrjl : jcyFybxdgrjls) {
                    if (StringUtils.isBlank(jcyFybxdgrjl.getFlwfsbje())){
                        throw new BusinessException("劳务费费用不能为空");
                    }

                    if (StringUtils.isBlank(jcyFybxdgrjl.getFhsbzsbje())){
                        throw new BusinessException("伙食补助费用不能为空");
                    }

                    if (StringUtils.isBlank(jcyFybxdgrjl.getFsnjtbzje())){
                        throw new BusinessException("交通补助费用不能为空");
                    }

                }
            }


            //下一步交通费 触发这个接口
            nkBxJtxcdService.initxcd(fbxdid);



        } else if (nextState.equals(OaSpcyJcyFybxdConstant.STATE_ZSF)){
            //检查交通费
            List<NkBxJtxcd> nkBxJtxcdList = nkBxJtxcdService.list(new LambdaQueryWrapper<NkBxJtxcd>()
                    .eq(NkBxJtxcd::getFbxdid, fbxdid));

            if (CollectionUtils.isNotEmpty(nkBxJtxcdList)){
                for (NkBxJtxcd nkBxJtxcd : nkBxJtxcdList) {
                    if (StringUtils.isBlank(nkBxJtxcd.getFwjtfsbje())){
                        throw new BusinessException("交通费应付金额不能为空");
                    }
                }
            }

            //住宿费之后更新 交通费的金额？
            nkBxJtxcdService.jtfy(fbxdid);


            syncBxryZxlx(fbxdid); // 同步报销人员-住宿类型

            bean = syncFybxdInYwdwpc(bean, fbxdid); // 同步费用报销单-是否-有无单位派车

        }else if (nextState.equals(OaSpcyJcyFybxdConstant.STATE_HZBD)){
            //检查住宿费
            List<OaSpcyJcyFybxdgrjl> jcyFybxdgrjls = oaSpcyJcyFybxdgrjlService.list(new LambdaQueryWrapper<OaSpcyJcyFybxdgrjl>()
                    .eq(OaSpcyJcyFybxdgrjl::getFbxdid, fbxdid));

            if (CollectionUtils.isNotEmpty(jcyFybxdgrjls)){
                for (OaSpcyJcyFybxdgrjl jcyFybxdgrjl : jcyFybxdgrjls) {
                    if (StringUtils.isBlank(jcyFybxdgrjl.getFzxfybje())){
                        throw new BusinessException("住宿费应付金额不能为空");
                    }
                }
            }
            bean = getHzBxd(bean, fbxdid); // 计算费用

        }else if (nextState.equals(OaSpcyJcyFybxdConstant.STATE_WC)){


            OaSpcyJcyFybxd OaSpcyJcyFybxd = oaSpcyJcyFybxdService.getById(fbxdid);
            if (StringUtils.isBlank(OaSpcyJcyFybxd.getFbxdj())){
                throw new BusinessException("报销凭据数量不能为空");
            }

            bean = getHzBxd(bean, fbxdid); // 计算费用

            initGenFybxxmmxJl(bean, fbxdid); // 生成报销明细

        }

        bean.setFbxdzt(nextState);

        if (StringUtils.isBlank(bean.getFdjh())) {
            try {
                bean.setFdjh(oaNkRsDjhglService.GetDocumentNumber(bean.getId()));
            } catch (Exception e) {
                log.error("检查员报销单-生成单据号错误: {}", e.getMessage());
            }
        }

        oaSpcyJcyFybxdService.updateById(bean);

        return HttpResult.ok("提交成功");
    }

    /**
     * 报销单信息-退回
     */
    public HttpResult returnFybxd(String fbxdid){
        if (StringUtils.isBlank(fbxdid)){
            return HttpResult.error("参数为空！");
        }

        OaSpcyJcyFybxd bean = oaSpcyJcyFybxdService.getById(fbxdid);
        if (bean == null){
            return HttpResult.error("没有相匹配的数据！");
        }

        bean.setFbxdzt(OaSpcyJcyFybxdConstant.getPreviousState(bean.getFbxdzt()));
        oaSpcyJcyFybxdService.updateById(bean);

        return HttpResult.ok("返回成功");
    }

    /**
     * 批量设置出行方式
     */
    public void batchSetBxdCxfsByBxdId(String fbxdid, String fcsjjtfs){
        if (StringUtils.isBlank(fbxdid)){
            throw new BusinessException("参数为空！");
        }
        if (StringUtils.isBlank(fcsjjtfs)){
            throw new BusinessException("出行方式为空！");
        }
        List<OaSpcyJcyFybxdgrjl> list = oaSpcyJcyFybxdgrjlService.list(new LambdaQueryWrapper<OaSpcyJcyFybxdgrjl>()
                .eq(OaSpcyJcyFybxdgrjl::getFbxdid, fbxdid));

        if (CollectionUtil.isNotEmpty(list)) {
            for (OaSpcyJcyFybxdgrjl grjl : list) {
                grjl.setFcsjjtfs(fcsjjtfs);

                if (fcsjjtfs.equals(OaSpcyJcyFybxdConstant.CSJJTFS_DEFAULT_DWPC)) {
                    grjl.setFfjtfdfrid("");
                    grjl.setFfjtfdfrxm("");
                    grjl.setFfjtfddcs("");
                    grjl.setFfjtfqscs("");
                    grjl.setFfjtfsbje("0");
                    grjl.setFfjtfybje("0");
                    grjl.setFfjtfsfdf(OaSpcyJcyFybxdConstant.FYBXD_SFDF_NO);

                    grjl.setFwjtfdfrid("");
                    grjl.setFwjtfdfrxm("");
                    grjl.setFwjtfddcs("");
                    grjl.setFwjtfqscs("");
                    grjl.setFwjtfsbje("0");
                    grjl.setFwjtfybje("0");
                    grjl.setFwjtfsfdf(OaSpcyJcyFybxdConstant.FYBXD_SFDF_NO);
                }

                oaSpcyJcyFybxdgrjlService.updateById(grjl);
            }
        }

    }

    /**
     * 批量设置劳务费/伙食/交通补助天数
     * @param fbxdid 报销单id
     * @param fjcts 天数
     */
    public void batchSzLwHsJtbzTs(String fbxdid, String fjcts){
        if (StringUtils.isBlank(fbxdid)){
            throw new BusinessException("参数为空！");
        }
        if (StringUtils.isBlank(fjcts)){
            throw new BusinessException("天数为空！");
        }
        Integer jcts = Integer.valueOf(fjcts);
        List<OaSpcyJcyFybxdgrjl> list = oaSpcyJcyFybxdgrjlService.list(new LambdaQueryWrapper<OaSpcyJcyFybxdgrjl>()
                .eq(OaSpcyJcyFybxdgrjl::getFbxdid, fbxdid));

        if (CollectionUtil.isNotEmpty(list)) {
            for (OaSpcyJcyFybxdgrjl grjl : list) {

//                grjl = jsBxdmxLwfHsfJtbz(grjl, jcts, jcts, jcts);
                grjl = jsBxdmxLwfHsfJtbz(grjl, jcts, jcts);
//                grjl.setFlwfts(jcts);
                grjl.setFhsbzts(jcts);
                grjl.setFsnjtbzts(jcts);

                oaSpcyJcyFybxdgrjlService.updateById(grjl);
            }
        }
    }

    /**
     * 同步报销人员-住宿类型
     */
    private void syncBxryZxlx(String fbxdid){
        List<OaSpcyJcyFybxdgrjl> list = oaSpcyJcyFybxdgrjlService.list(new LambdaQueryWrapper<OaSpcyJcyFybxdgrjl>()
                .eq(OaSpcyJcyFybxdgrjl::getFbxdid, fbxdid));

        if (CollectionUtil.isNotEmpty(list)) {
            for (OaSpcyJcyFybxdgrjl grjl : list) {
                // 是否存在特殊事项申请
                List<OaSpcyJcyFybxdtssxsq> tssxsqList = OaSpcyJcyFybxdtssxsqService.list(new LambdaQueryWrapper<OaSpcyJcyFybxdtssxsq>()
                        .eq(OaSpcyJcyFybxdtssxsq::getFsqlx, OaSpcyJcyFybxdConstant.FYBXD_TSSXSQ_SQLX_WXF)
                        .like(OaSpcyJcyFybxdtssxsq::getFwxfsqrid, grjl.getFuserid()));
                if (CollectionUtil.isNotEmpty(tssxsqList)){
                    grjl.setFzxlx(OaSpcyJcyFybxdConstant.FYBXD_BXRY_ZXLX_WX);
                }else {
                    grjl.setFzxlx(OaSpcyJcyFybxdConstant.FYBXD_BXRY_ZXLX_QT);
                }
                oaSpcyJcyFybxdgrjlService.updateById(grjl);
            }
        }
    }

    /**
     * 同步费用报销单-是否-有无单位派车
     */
    private OaSpcyJcyFybxd syncFybxdInYwdwpc(OaSpcyJcyFybxd fybxd, String fbxdid){
        StringBuilder qzrid = new StringBuilder();
        StringBuilder qzrxm = new StringBuilder();
        String type = OaSpcyJcyFybxdConstant.FYBXD_SFYWDWPC_NO;

        List<OaSpcyJcyFybxdgrjl> list = oaSpcyJcyFybxdgrjlService.list(new LambdaQueryWrapper<OaSpcyJcyFybxdgrjl>()
                .eq(OaSpcyJcyFybxdgrjl::getFbxdid, fbxdid)
                .eq(OaSpcyJcyFybxdgrjl::getFcsjjtfs, OaSpcyJcyFybxdConstant.CSJJTFS_DEFAULT_WDWPC));
        if (CollectionUtil.isNotEmpty(list)){
            type = OaSpcyJcyFybxdConstant.FYBXD_SFYWDWPC_YES;

            for (OaSpcyJcyFybxdgrjl grjl : list){
                if (!StringPatternUtils.verifryIsEmptyStringBuilder(qzrid)){
                    qzrid.append(",");
                    qzrxm.append(",");
                }

                qzrid.append(grjl.getFuserid());
                qzrxm.append(grjl.getRealname());
            }
        }

        fybxd.setFsfywdwpc(type);
        fybxd.setFwcsjtzssmqzrid(qzrid.toString());
        fybxd.setFwcsjtzssmqzrxm(qzrxm.toString());

        return fybxd;
    }

    /**
     * 验证报销人员-是否填写签名
     */
    private void verifyIsExistsFybxdBxryInUserQm(String fbxdid){
        List<OaSpcyJcyFybxdgrjl> list = oaSpcyJcyFybxdgrjlService.list(new LambdaQueryWrapper<OaSpcyJcyFybxdgrjl>()
                .eq(OaSpcyJcyFybxdgrjl::getFbxdid, fbxdid));

        if (CollectionUtil.isNotEmpty(list)){
            for (OaSpcyJcyFybxdgrjl grjl : list) {
                if (StringUtils.isNotBlank(grjl.getFuserid())) {
                    SpcySqsxWjjl signWjjl = spcySqsxWjjlService.downMyUserQm(grjl.getFuserid(), null);
                    if (signWjjl == null){
                        throw new BusinessException("【"+grjl.getRealname()+"】没有上传相关签名附件！");
                    }
                }
            }
        }else {
            throw new BusinessException("报销人员列表为空！");
        }
    }

    /**
     * 重新生成汇总表-对应处理 汇总表单环节进行重新计算
     */
    private void againHzBxd(String fbxdid){
        if (StringUtils.isBlank(fbxdid)){
            return;
        }

        String bxdzt = oaSpcyJcyFybxdMapper.selectFbxdztById(fbxdid);
        if (StringUtils.isBlank(bxdzt)){
            return;
        }

        if (!bxdzt.equals(OaSpcyJcyFybxdConstant.STATE_HZBD)){
            return;
        }

        OaSpcyJcyFybxd fybxd = new OaSpcyJcyFybxd();
        fybxd.setFlwfbxje("0");
        fybxd.setFclfbxje("0");
        fybxd.setFhjbxje("0");

        /** 重新计算相关金额 */
        fybxd = this.getHzBxd(fybxd, fbxdid);

        /** 更新相关金额 */
        LambdaUpdateWrapper<OaSpcyJcyFybxd> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(OaSpcyJcyFybxd::getId, fbxdid);
        /*OaSpcyJcyFybxd updateBean = new OaSpcyJcyFybxd();
        updateBean.setFlwfbxje(fybxd.getFlwfbxje());
        updateBean.setFclfbxje(fybxd.getFclfbxje());
        updateBean.setFhjbxje(fybxd.getFhjbxje());*/
        oaSpcyJcyFybxdService.update(fybxd, updateWrapper);

    }

    /**
     * 生成汇总表（计算劳务费和差旅费 应报、实报和合计金额）
     * @param fybxd
     * @param fbxdid
     * @return
     */
    private OaSpcyJcyFybxd getHzBxd(OaSpcyJcyFybxd fybxd, String fbxdid){
        BigDecimal bxd_flwfbxje = new BigDecimal("0");
        BigDecimal bxd_fclfbxje = new BigDecimal("0");
        BigDecimal bxd_hsbz = new BigDecimal("0");
        BigDecimal bxd_csjjtf = new BigDecimal("0");
        BigDecimal bxd_snjtf = new BigDecimal("0");
        BigDecimal bxd_zxf = new BigDecimal("0");

        List<OaSpcyJcyFybxdgrjl> bxmxList = queryBxryxxListByBxdId(fbxdid);
        if (CollectionUtil.isNotEmpty(bxmxList)){
            for (OaSpcyJcyFybxdgrjl bxmx : bxmxList){
                // 劳务费
                int lwfts = Optional.ofNullable(bxmx.getFlwfts()).orElseGet(()->0);
                // 伙食补助
                int hsbzts = Optional.ofNullable(bxmx.getFhsbzts()).orElseGet(()->0);
                // 市内交通补助
                int snjtbzts = Optional.ofNullable(bxmx.getFsnjtbzts()).orElseGet(()->0);

                //计算劳务费/伙食费/交通补助

                if (bxmx.getFywlwf().equals("无")){
                    bxmx = jsBxdmxLwfHsfJtbz(bxmx, hsbzts, snjtbzts);
                }else{
                    bxmx = jsBxdmxLwfHsfJtbz(bxmx, lwfts, hsbzts, snjtbzts);
                }


                /**
                 * 这里就不需要在计算交通费了，交通费计算 由 交通行程那边计算之后 直接赋值给 报销单个人记录表 之后给汇总表
                 */

                /**
                 * 城市间交通费-往-实报金额
                 * 如果本人选择是垫付 实报金额为0，把本人的合计金额加到垫付人的实报金额
                 */
//                if (StringUtils.isNotBlank(bxmx.getFwjtfsfdf()) && bxmx.getFwjtfsfdf().equals("是")){
//                    bxmx.setFwjtfsbje("0");
//                }else {
//                    bxmx.setFwjtfsbje(bxmx.getFwjtfybje());
//                }

                /**
                 * 城市间交通费-返-实报金额
                 * 如果本人选择是垫付 实报金额为0，把本人的合计金额加到垫付人的实报金额
                 */
//                if (StringUtils.isNotBlank(bxmx.getFfjtfsfdf()) && bxmx.getFfjtfsfdf().equals("是")){
//                    bxmx.setFfjtfsbje("0");
//                }else {
//                    bxmx.setFfjtfsbje(bxmx.getFfjtfybje());
//                }

                /**
                 *  住宿费实报金额
                 * 如果本人选择是垫付 实报金额为0，把本人的合计金额加到垫付人的实报金额
                 */
                if (StringUtils.isNotBlank(bxmx.getFzxfsfdf()) && bxmx.getFzxfsfdf().equals("是")){
                    bxmx.setFzxfsbje("0");
                }else {
                    bxmx.setFzxfsbje(bxmx.getFzxfybje());
                }

                /**
                 * 查询垫付-城市间交通费-往-垫付人id
                 * 计算垫付实报金额
                 */
//                List<OaSpcyJcyFybxdgrjl> wjtfList = oaOaSpcyJcyFybxdgrjlService.list(new LambdaQueryWrapper<OaSpcyJcyFybxdgrjl>()
//                        .eq(OaSpcyJcyFybxdgrjl::getFwjtfdfrid, bxmx.getFuserid())
//                        .eq(OaSpcyJcyFybxdgrjl::getFwjtfsfdf, "是"));
//                BigDecimal wjtfTotal = new BigDecimal(bxmx.getFwjtfsbje());
//                for (OaSpcyJcyFybxdgrjl w : wjtfList){
//                    wjtfTotal = wjtfTotal.add(new BigDecimal(Optional.ofNullable(w.getFwjtfybje()).orElseGet(()->"0")));
//                    bxmx.setFwjtfsbje(wjtfTotal.toString());
//                }

                /**
                 * 城市间交通费-返-垫付人id
                 * 计算垫付实报金额
                 */
//                List<OaSpcyJcyFybxdgrjl> fjtfList = oaOaSpcyJcyFybxdgrjlService.list(new LambdaQueryWrapper<OaSpcyJcyFybxdgrjl>()
//                        .eq(OaSpcyJcyFybxdgrjl::getFfjtfdfrid, bxmx.getFuserid())
//                        .eq(OaSpcyJcyFybxdgrjl::getFfjtfsfdf, "是"));
//                BigDecimal fjtfTotal = new BigDecimal(bxmx.getFfjtfsbje());
//                for (OaSpcyJcyFybxdgrjl f : fjtfList){
//                    fjtfTotal = fjtfTotal.add(new BigDecimal(Optional.ofNullable(f.getFfjtfybje()).orElseGet(()->"0")));
//                    bxmx.setFfjtfsbje(fjtfTotal.toString());
//                }

                /**
                 * 住宿费垫付人id
                 * 计算垫付实报金额
                 */
                List<OaSpcyJcyFybxdgrjl> zxfList = oaSpcyJcyFybxdgrjlService.list(new LambdaQueryWrapper<OaSpcyJcyFybxdgrjl>()
                        .eq(OaSpcyJcyFybxdgrjl::getFzxfdfrid, bxmx.getFuserid())
                        .eq(OaSpcyJcyFybxdgrjl::getFzxfsfdf, "是"));
                BigDecimal zxfTotal = new BigDecimal(Optional.ofNullable(bxmx.getFzxfsbje()).orElseGet(()->"0"));
                for (OaSpcyJcyFybxdgrjl z : zxfList){
                    zxfTotal = zxfTotal.add(new BigDecimal(Optional.ofNullable(z.getFzxfybje()).orElseGet(()->"0")));
                    bxmx.setFzxfsbje(zxfTotal.toString());
                }

                //特殊事项报销金额
                String tssxsqBxJe = this.jcyFybxdTssxsqBxJe(bxmx.getFbxdid(), bxmx.getFuserid());


                /** 差旅费应报合计金额 */
                BigDecimal clfYbje = new BigDecimal("0");
                clfYbje = clfYbje.add(new BigDecimal(Optional.ofNullable(bxmx.getFhsbzsbje()).orElseGet(()->"0"))); // 伙食补助
                clfYbje = clfYbje.add(new BigDecimal(Optional.ofNullable(bxmx.getFsnjtbzje()).orElseGet(()->"0"))); // 市内交通

                clfYbje = clfYbje.add(new BigDecimal(Optional.ofNullable(bxmx.getFwjtfybje()).orElseGet(()->"0"))); // 往-交通费-应报金额
//                clfYbje = clfYbje.add(new BigDecimal(bxmx.getFfjtfybje())); // 返-交通费-应报金额
                clfYbje = clfYbje.add(new BigDecimal(Optional.ofNullable(bxmx.getFzxfybje()).orElseGet(()->"0"))); // 住宿费-应报金额
                if(StringUtils.isNotBlank(tssxsqBxJe)){



                    clfYbje = clfYbje.add(new BigDecimal(tssxsqBxJe));
                }

                bxmx.setFclfybhjje(clfYbje.toString());

                /** 差旅费实报合计金额 */
                BigDecimal clfSbje = new BigDecimal("0");
                clfSbje = clfSbje.add(new BigDecimal(bxmx.getFhsbzsbje())); // 伙食补助
                clfSbje = clfSbje.add(new BigDecimal(bxmx.getFsnjtbzje())); // 伙食补助

                clfSbje = clfSbje.add(new BigDecimal(Optional.ofNullable(bxmx.getFwjtfsbje()).orElseGet(()->"0"))); // 往-交通费-实报金额
//                clfSbje = clfSbje.add(new BigDecimal(bxmx.getFfjtfsbje())); // 返-交通费-实报金额
                clfSbje = clfSbje.add(new BigDecimal(Optional.ofNullable(bxmx.getFzxfsbje()).orElseGet(()->"0"))); // 住宿费-实报金额

                if(StringUtils.isNotBlank(tssxsqBxJe)){
                    clfSbje = clfSbje.add(new BigDecimal(tssxsqBxJe));
                }

                bxmx.setFclfsbhjje(clfSbje.toString());

                String hj_flwfsbje = Optional.ofNullable(bxmx.getFlwfsbje()).orElseGet(()->"0"); // 劳务费合计实报金额
                String hj_fclfsbje = bxmx.getFclfsbhjje(); // 差旅费合计实报金额

                String lwfsqje = "0";
                String lwfgsje = "0";
                String lwfsfjgs = OaSpcyJcyFybxdConstant.FYBXD_LWFSFJGS_NO;
                if (StringUtils.isNotBlank(bxmx.getFlwfsbje())){

                    /**
                     * 应报劳务费 > 800 < 4000
                     * 劳务费税前金额 = （ 应报劳务费 - 160 ）/ 0.8
                     * 个税 = (劳务费税前金额 - 800 ）* 20%
                     */
                    BigDecimal sbjr = new BigDecimal(bxmx.getFlwfsbje());
                    if (sbjr.compareTo(new BigDecimal("800")) == 1){
                        BigDecimal subtract = sbjr.subtract(new BigDecimal("160"));
                        BigDecimal lwfsqjeBd = subtract.divide(new BigDecimal("0.8")); // 劳务费税前金额
                        lwfsqje = lwfsqjeBd.toPlainString(); // 劳务费税前金额

                        lwfgsje = lwfsqjeBd.subtract(sbjr).toPlainString(); // 个税
                        lwfsfjgs = OaSpcyJcyFybxdConstant.FYBXD_LWFSFJGS_YES;

                        hj_flwfsbje = lwfsqje;  // 劳务费 = 劳务费税前金额
                        //劳务费 = 劳务费税前金额 - 个税
                        hj_flwfsbje = String.valueOf(new BigDecimal(lwfsqje).subtract(new BigDecimal(lwfgsje)));


                    }
                }else {
                    bxmx.setFlwfsbje("0");
                }
                bxmx.setFlwfgsje(lwfgsje); // 劳务费个税金额
                bxmx.setFlwfsqje(lwfsqje);  // 劳务费税前金额
                bxmx.setFlwfsfjgs(lwfsfjgs); // 是否交个税

                oaSpcyJcyFybxdgrjlService.updateById(bxmx);



                // 城市间交通费小计
                BigDecimal xj_csjjtf = new BigDecimal(Optional.ofNullable(bxmx.getFwjtfybje()).orElse("0"));
                bxd_csjjtf = bxd_csjjtf.add(xj_csjjtf);

                bxd_snjtf = bxd_snjtf.add(new BigDecimal(bxmx.getFsnjtbzje())); // 市内交通费小计
                bxd_hsbz = bxd_hsbz.add(new BigDecimal(bxmx.getFhsbzsbje())); // 伙食补助小计
                bxd_zxf = bxd_zxf.add(new BigDecimal(bxmx.getFzxfsbje())); // 住宿费小计




                bxd_flwfbxje = bxd_flwfbxje.add(new BigDecimal(hj_flwfsbje)); // 劳务费合计报销金额
                // 差旅费合计报销金额
                bxd_fclfbxje = bxd_fclfbxje.add(new BigDecimal(hj_fclfsbje));

            }
        }

        /** 计算报销单-合计劳务费和差旅费 */
        fybxd.setFlwfbxje(bxd_flwfbxje.toString());  // 报销单-劳务费合计报销金额
        fybxd.setFclfbxje(bxd_fclfbxje.toString()); // 报销单-差旅费合计报销金额

        fybxd.setFhsbzxj(bxd_hsbz.toString()); // 伙食补助小计
        fybxd.setFjtfxj(bxd_csjjtf.toString()); // 城市间交通费小计
        fybxd.setFsnjtfxj(bxd_snjtf.toString()); // 市内交通费小计
        fybxd.setFzsfxj(bxd_zxf.toString()); // 住宿费小计

        BigDecimal bxdTotal = new BigDecimal("0");  // 合计报销金额
        bxdTotal = bxdTotal.add(new BigDecimal(bxd_flwfbxje.toString()));
        bxdTotal = bxdTotal.add(new BigDecimal(bxd_fclfbxje.toString()));

        //如果转账申请单 有数据 那就要加上转账申请单的数据
        String zzsqdje = this.zzsqdje(fbxdid);
        if (StringUtils.isNotBlank(zzsqdje)){
            bxdTotal = bxdTotal.add(new BigDecimal(zzsqdje));
        }


        fybxd.setFhjbxje(bxdTotal.toString());

        /** 金额大写 */
        try {
            fybxd.setFhjbxjedx(ChineseMoneyUtils.toChineseMoney(bxdTotal));
        }catch (Exception e){
            log.error("检查员报销单合计计算大写金额错误：{}", e.getMessage());
        }


        return fybxd;
    }


    /**
     * 转账申请单金额
     */
    private String zzsqdje(String fbxdid) {
        List<NkRsZzsqd> nkRsZzsqds = nkRsZzzsqdService.list(new LambdaQueryWrapper<NkRsZzsqd>()
                .eq(NkRsZzsqd::getFbxdid, fbxdid));
        BigDecimal zzzje = new BigDecimal(0);//转账总金额

        for (NkRsZzsqd nkRsZzsqd:nkRsZzsqds){
            zzzje = zzzje.add(new BigDecimal(nkRsZzsqd.getFjexx()));
        }
        return String.valueOf(zzzje);

    }



    /**
     * 计算特殊事项申请报销金额
     * @return
     */
    private String jsFybxdTssxsqBxJe(String fbxdid){
        BigDecimal total = new BigDecimal("0");

        List<OaSpcyJcyFybxdtssxsq> list = OaSpcyJcyFybxdtssxsqService.list(new LambdaQueryWrapper<OaSpcyJcyFybxdtssxsq>()
                .eq(OaSpcyJcyFybxdtssxsq::getFbxdid, fbxdid));
        if (CollectionUtil.isNotEmpty(list)){
            for (OaSpcyJcyFybxdtssxsq sq : list){
                total = total.add(new BigDecimal(Optional.ofNullable(sq.getFsqje()).orElseGet(()->"0")));
            }
        }

        return total.toString();
    }


    /**
     * 获取当前人的特殊事项金额
     */
    private String jcyFybxdTssxsqBxJe(String fbxdid ,String userid){
        BigDecimal ewtotal = new BigDecimal("0");
        BigDecimal wxtotal = new BigDecimal("0");


        List<OaSpcyJcyFybxdtssxsq> list = OaSpcyJcyFybxdtssxsqService.list(new LambdaQueryWrapper<OaSpcyJcyFybxdtssxsq>()
                .eq(OaSpcyJcyFybxdtssxsq::getFbxdid, fbxdid)
                .eq(OaSpcyJcyFybxdtssxsq::getFlczt, OaCommonFlowConstant.FLOW_LCJS));
        //如果存在特殊事项申请，查看申请人字符串的长度，将报销金额除以申请人的长度？
        if (CollectionUtil.isNotEmpty(list)){
            List<String> ewfylist = new ArrayList();
            List<String> wxflist = new ArrayList();




            for (OaSpcyJcyFybxdtssxsq sq : list){
                if (sq.getFsqlx().equals(OaSpcyJcyFybxdConstant.FYBXD_TSSXSQ_SQLX_CQCS)){
                    String fewfybxsqrid = sq.getFewfybxsqrid();
                    ewfylist = Arrays.asList(fewfybxsqrid.split(",|，"));



                    ewtotal = ewtotal.add(new BigDecimal(Optional.ofNullable(sq.getFsqje()).orElseGet(()->"0")));

                }
                if(sq.getFsqlx().equals(OaSpcyJcyFybxdConstant.FYBXD_TSSXSQ_SQLX_WXF)){
                    String fwxflist = sq.getFwxfsqrid();
                    wxflist = Arrays.asList(fwxflist.split(",|，"));



                    wxtotal = wxtotal.add(new BigDecimal(Optional.ofNullable(sq.getFsqje()).orElseGet(()->"0")));

                }
            }
            if (CollectionUtils.isNotEmpty(ewfylist)){

                boolean isPresent = ewfylist.contains(userid);
                //如果存在 看这个ew的list长度 然后 除以长度 得到 ew的金额
                if (isPresent){
                    int size = ewfylist.size();
                    BigDecimal divide = ewtotal.divide(new BigDecimal(size),3, RoundingMode.HALF_UP);
                    return String.valueOf(divide);
                }
            }

            if (CollectionUtils.isNotEmpty(wxflist)){

                boolean isPresent = wxflist.contains(userid);
                //如果存在 看这个ew的list长度 然后 除以长度 得到 ew的金额
                if (isPresent){
                    int size = wxflist.size();
                    BigDecimal divide = wxtotal.divide(new BigDecimal(size),3, RoundingMode.HALF_UP);
                    return String.valueOf(divide);
                }
            }
        }

        return "";
    }


    /**
     * 计算劳务费/伙食费/交通补助
     * @param bxmx
     * @param lwfts
     * @param hsbzts
     * @param snjtbzts
     * @return
     */
    private OaSpcyJcyFybxdgrjl jsBxdmxLwfHsfJtbz(OaSpcyJcyFybxdgrjl bxmx, int lwfts, int hsbzts, int snjtbzts){
        String flwfbz = bxmx.getFlwfbz();

        if (StringUtils.isNotBlank(bxmx.getFjcsf())) {
            if (bxmx.getFjcsf().contains(SpcyConstant.HCY_SSK_ZZ)) {
                bxmx.setFlwfsbje(String.valueOf(new BigDecimal(lwfts).multiply(new BigDecimal(flwfbz)))); // 劳务费
//                bxmx.setFlwfsbje(String.valueOf(lwfts * 300)); // 劳务费

                bxmx.setFhsbzsbje(String.valueOf(hsbzts * 100)); // 伙食补助
                bxmx.setFsnjtbzje(String.valueOf(snjtbzts * 80)); // 市内交通补助

            } else if (bxmx.getFjcsf().contains(SpcyConstant.HCY_SSK_ZY)) {
                if (StringUtils.isBlank(flwfbz)){
                    flwfbz = "300";
                }
                bxmx.setFlwfsbje(String.valueOf(new BigDecimal(lwfts).multiply(new BigDecimal(flwfbz)))); // 劳务费

//                bxmx.setFlwfsbje(String.valueOf(lwfts * 300)); // 劳务费
                bxmx.setFhsbzsbje(String.valueOf(hsbzts * 100)); // 伙食补助
                bxmx.setFsnjtbzje(String.valueOf(snjtbzts * 80)); // 市内交通补助

            }else if (bxmx.getFjcsf().contains(SpcyConstant.HCY_SSK_YB)){
                bxmx.setFlwfsbje("0"); // 劳务费
                bxmx.setFhsbzsbje(String.valueOf(hsbzts * 100)); // 伙食补助
                bxmx.setFsnjtbzje(String.valueOf(snjtbzts * 80)); // 市内交通补助

            }
//            else {
//                bxmx.setFlwfsbje("0");
//                bxmx.setFhsbzsbje("0");
//                bxmx.setFsnjtbzje("0");
//            }
        }else {
            bxmx.setFlwfsbje("0");
            bxmx.setFhsbzsbje("0");
            bxmx.setFsnjtbzje("0");

        }

        // 住宿费 = 劳务费+1
        if (bxmx.getFzxfts() == null){
            bxmx.setFzxfts(lwfts+1);
        }




        return bxmx;
    }


    private OaSpcyJcyFybxdgrjl jsBxdmxLwfHsfJtbz(OaSpcyJcyFybxdgrjl bxmx,  int hsbzts, int snjtbzts){
        if (StringUtils.isNotBlank(bxmx.getFjcsf())) {
            if (bxmx.getFjcsf().contains(SpcyConstant.HCY_SSK_ZZ)) {
                bxmx.setFhsbzsbje(String.valueOf(hsbzts * 100)); // 伙食补助
                bxmx.setFsnjtbzje(String.valueOf(snjtbzts * 80)); // 市内交通补助

            } else if (bxmx.getFjcsf().contains(SpcyConstant.HCY_SSK_ZY)) {
                bxmx.setFhsbzsbje(String.valueOf(hsbzts * 100)); // 伙食补助
                bxmx.setFsnjtbzje(String.valueOf(snjtbzts * 80)); // 市内交通补助

            }else if (bxmx.getFjcsf().contains(SpcyConstant.HCY_SSK_YB)) {
                bxmx.setFlwfsbje("0"); // 劳务费
                bxmx.setFhsbzsbje(String.valueOf(hsbzts * 100)); // 伙食补助
                bxmx.setFsnjtbzje(String.valueOf(snjtbzts * 80)); // 市内交通补助
            }

//            }else {
//                bxmx.setFlwfsbje("0");
//                bxmx.setFhsbzsbje("0");
//                bxmx.setFsnjtbzje("0");
//            }
        }else {
            bxmx.setFlwfsbje("0");
            bxmx.setFhsbzsbje("0");
            bxmx.setFsnjtbzje("0");

        }




        return bxmx;
    }


    /**
     * 初始化生成-费用报销项目明细记录
     */
    private void initGenFybxxmmxJl(OaSpcyJcyFybxd fybxd, String fbxdid) {
        List<OaSpcyJcyFybxxmmxjl> spcyJcyFybxxmmxjls = oaSpcyJcyFybxxmmxjlService.list(new LambdaQueryWrapper<OaSpcyJcyFybxxmmxjl>()
                .eq(OaSpcyJcyFybxxmmxjl::getFbxdid, fbxdid));
        if (CollectionUtil.isNotEmpty(spcyJcyFybxxmmxjls)){

            oaSpcyJcyFybxxmmxjlService.removeBatchByIds(spcyJcyFybxxmmxjls);

        }


        if (StringUtils.isBlank(fbxdid)){
            return;
        }

        // 报销单
        if (fybxd == null) {
            fybxd = oaSpcyJcyFybxdService.getById(fbxdid);
            if (fybxd == null) {
                return;
            }
        }

        // 报销单人员记录
        List<OaSpcyJcyFybxdgrjl> bxgrjlList = oaSpcyJcyFybxdgrjlService.list(new LambdaQueryWrapper<OaSpcyJcyFybxdgrjl>()
                .eq(OaSpcyJcyFybxdgrjl::getFbxdid, fbxdid));

        if (CollectionUtil.isEmpty(bxgrjlList)){
            return;
        }

        // 生成报销项目明细
        for (OaSpcyJcyFybxdgrjl grjl : bxgrjlList){

            // 同步修改银行卡/公务卡
//            this.syncUserYkhOrGwk(grjl);

            List<String> bxxmflList = CollectionUtil.newArrayList(OaSpcyJcyFybxdConstant.BXXMFL_LWF, OaSpcyJcyFybxdConstant.BXXMFL_CLF);

            OaSpcyJcyFybxd finalFybxd = fybxd;
            bxxmflList.forEach(e -> {
                OaSpcyJcyFybxxmmxjl bxmxxm = new OaSpcyJcyFybxxmmxjl();

                ConvertUtils.register(new DateConvert(), Date.class);
                try {
                    BeanUtils.copyProperties(bxmxxm, finalFybxd);
                    BeanUtils.populate(bxmxxm, BeanUtils.describe(grjl));
                } catch (IllegalAccessException ex) {
                    log.error("[IllegalAccessException]初始化生成-费用报销项目明细记录: {}", ex.getMessage());
                } catch (InvocationTargetException ex) {
                    log.error("[InvocationTargetException]初始化生成-费用报销项目明细记录: {}", ex.getMessage());
                } catch (NoSuchMethodException ex) {
                    log.error("[NoSuchMethodException]初始化生成-费用报销项目明细记录: {}", ex.getMessage());
                }

                bxmxxm.setFbxxmfl1(e);
                if (e.equals(OaSpcyJcyFybxdConstant.BXXMFL_LWF)) {
                    bxmxxm.setFybhjfy(grjl.getFlwfsbje());
                    bxmxxm.setFsbhjfy(grjl.getFlwfsbje());

                }else if (e.equals(OaSpcyJcyFybxdConstant.BXXMFL_CLF)){
                    bxmxxm.setFybhjfy(grjl.getFclfybhjje());
                    bxmxxm.setFsbhjfy(grjl.getFclfsbhjje());
                }

                bxmxxm.setId(GetUuIdUtils.ReplaceUuId());
                bxmxxm.setFbxdgrjlid(grjl.getId());
                bxmxxm.setFbxdid(finalFybxd.getId());
                bxmxxm.setFsqsxid(finalFybxd.getFsqsxid());
                bxmxxm.setFuserid(grjl.getFuserid());
                bxmxxm.setFqydz(finalFybxd.getFqydz());
                oaSpcyJcyFybxxmmxjlService.save(bxmxxm);

            });

        }

    }

    /**
     * 费用报销项目明细记录-分页查询
     */
    public HttpResult queryFybxxmmxPageList(OaSpcyJcyFybxxmmxjl query, String gjz, PageVo pageVo, OrderItemVo orderItemVo){

        Page<OaSpcyJcyFybxxmmxjl> page = new Page<>(pageVo.getPageNo(), pageVo.getPageSize());

        LambdaQueryWrapper<OaSpcyJcyFybxxmmxjl> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(query.getFbxnrms()), OaSpcyJcyFybxxmmxjl::getFbxnrms, query.getFbxnrms());
        queryWrapper.like(StringUtils.isNotBlank(query.getRealname()), OaSpcyJcyFybxxmmxjl::getRealname, query.getRealname());
        queryWrapper.like(StringUtils.isNotBlank(query.getFkhh()), OaSpcyJcyFybxxmmxjl::getFkhh, query.getFkhh());
        queryWrapper.like(StringUtils.isNotBlank(query.getFjcqymc()), OaSpcyJcyFybxxmmxjl::getFjcqymc, query.getFjcqymc());
        queryWrapper.like(StringUtils.isNotBlank(query.getFjcsxmc()), OaSpcyJcyFybxxmmxjl::getFjcsxmc, query.getFjcsxmc());
        queryWrapper.like(StringUtils.isNotBlank(query.getFgzdw()), OaSpcyJcyFybxxmmxjl::getFgzdw, query.getFgzdw());
        queryWrapper.like(StringUtils.isNotBlank(query.getFbxxmfl1()), OaSpcyJcyFybxxmmxjl::getFbxxmfl1, query.getFbxxmfl1());

        if (StringUtils.isNotBlank(gjz)){
            int selectSex = 3;
            if (gjz.contains("男")){
                selectSex = 1;
            }else if (gjz.contains("女")){
                selectSex = 2;
            }
            Integer finalSelectSex = selectSex;

            queryWrapper
                    .and(f->f.like(OaSpcyJcyFybxxmmxjl::getRealname, gjz)
                            .or()
                            .like(OaSpcyJcyFybxxmmxjl::getFbxnrms, gjz)
                            .or()
                            .like(OaSpcyJcyFybxxmmxjl::getFkhh, gjz)
                            .or()
                            .like(OaSpcyJcyFybxxmmxjl::getFjcqymc, gjz)
                            .or()
                            .like(OaSpcyJcyFybxxmmxjl::getFjcsxmc, gjz)
                            .or()
                            .like(OaSpcyJcyFybxxmmxjl::getFbxxmfl1, gjz)
                            .or()
                            .like(OaSpcyJcyFybxxmmxjl::getFgzdw, gjz)
                            .or()
                            .eq(OaSpcyJcyFybxxmmxjl::getSex, finalSelectSex)
                    );
        }

        Page<OaSpcyJcyFybxxmmxjl> pageList = oaSpcyJcyFybxxmmxjlService.page(page, queryWrapper);

        return HttpResult.ok(pageList);
    }

    /**
     * 特殊事项申请-分页查询
     */
    public HttpResult queryFybxdTssxSqPageList(String fbxdid, PageVo pageVo){
        if (StringUtils.isBlank(fbxdid)){
            fbxdid = "0";
        }
//        Page<OaSpcyJcyFybxdtssxsq> page = new Page<>(pageVo.getPageNo(), pageVo.getPageSize());

        List<JcyfybxdTssxsqVo> sfkch = oaSpcyJcyFybxdtssxsqMapper.sfkch(fbxdid);


        return HttpResultVo.HttpResultList(sfkch);
    }

    /**
     * 特殊事项申请-根据id查看
     */
    public OaSpcyJcyFybxdtssxsq queryFybxdTssxSqById(String id){
        if (StringUtils.isBlank(id)){
            throw new BusinessException("参数为空！");
        }
        return OaSpcyJcyFybxdtssxsqService.getById(id);
    }

    /**
     * 新增-特殊事项申请
     */
    public OaSpcyJcyFybxdtssxsq addFybxdTssxSq(OaSpcyJcyFybxdtssxsq bean){
        if (StringUtils.isBlank(bean.getFbxdid())){
            throw new BusinessException("参数为空！");
        }

        if (StringUtils.isBlank(bean.getFsqlx())){
            throw new BusinessException("申请类型为空！");
        }

        if (bean.getFsqts() == null){
            bean.setFsqts(0);
        }

        OaSpcyJcyFybxd bxd = oaSpcyJcyFybxdService.getById(bean.getFbxdid());
        if (bxd == null){
            throw new BusinessException("没有相匹配的数据！");
        }

        // 额外费用
        if (StringUtils.isNotBlank(bean.getFewfybxsqrid())){
            List<YcSysUser> userList = oaSysUserService.findRealnameListByIds(StringPatternUtils.getListByIds(bean.getFewfybxsqrid()));
            String sqr = "";
            if (CollectionUtil.isNotEmpty(userList)){
                for (YcSysUser user : userList){
                    if (StringUtils.isNotBlank(sqr)){
                        sqr += "," + user.getRealname();
                    }else {
                        sqr += user.getRealname();
                    }
                }
            }
            bean.setFewfybxsqrxm(sqr);
        }

        // 午休
        if (StringUtils.isNotBlank(bean.getFwxfsqrid())){
            List<YcSysUser> userList = oaSysUserService.findRealnameListByIds(StringPatternUtils.getListByIds(bean.getFwxfsqrid()));
            String sqr = "";
            if (CollectionUtil.isNotEmpty(userList)){
                for (YcSysUser user : userList){
                    if (StringUtils.isNotBlank(sqr)){
                        sqr += "," + user.getRealname();
                    }else {
                        sqr += user.getRealname();
                    }
                }
            }
            bean.setFwxfsqrxm(sqr);
        }

        bean.setId(GetUuIdUtils.ReplaceUuId());

        if (StringUtils.isBlank(bean.getFsqrq())){
            bean.setFsqrq(DateConversionUtils.DateToStringYYYYMMDD(new Date()));
        }

        // 经办科室
        bean.setFsqrid(bxd.getFjbrid());
        bean.setFsqr(bxd.getFjbr());
        bean.setFsqks(bxd.getFjbks());

//        if (StringUtils.isNotBlank(bxd.getFsqsxid())) {
//            /*SpcySqsxBasic sqsx = spcySqsxBaiscService.getById(bxd.getFsqsxid());
//            if (sqsx != null && StringUtils.isNotBlank(sqsx.getFzbr())) {
//                YcSysUser zbr = sysUserService.getById(StringPatternUtils.getStringsByIds(sqsx.getFzbr())[0]);
//                if (zbr != null) {
//                    bean.setFsqrid(zbr.getId());
//                    bean.setFsqr(zbr.getRealname());
//                    bean.setFsqks(SpcyConstant.getYwclNameByCode(sqsx.getFywflcode()));
//                }
//            }*/
//        }

        /*bean.setFsqrid(GetCurrentUserUtils.getCurrentUserId());
        bean.setFsqr(GetCurrentUserUtils.getCurrentUserName());
        bean.setFsqks(GetCurrentUserUtils.getCurrentUserDeptName());*/

        bean.setFxmnr(bxd.getFbxnrms());
        if (StringUtils.isBlank(bean.getFxmsj())) {
            bean.setFxmsj(bxd.getFbxsjms());
        }

        bean.setFlczt(OaSpcyFlowConstant.LCZT_LCZB);

        if (StringUtils.isBlank(bean.getFsqje())){
            bean.setFsqje("0");
        }
        OaSpcyJcyFybxdtssxsqService.save(bean);

        // 报销特殊申请状态
        updateBxdTssxsq(bean.getFbxdid());

        String log = LogUtils.MapToStringMethod(ObjectToMapUtils.objectToMap(bean));
        spcyLogService.addLogs(LogConstant.LOGS_JCY_FYBXD,log,bean.getFbxdid(),"添加特殊事项申请", SpcyConstant.LOGS_SYS_TYPE_XZ);

        return bean;
    }

    /**
     * 更新-特殊事项申请
     */
    public void updateFybxdTssxSq(HashMap<String,String> buffer) throws IllegalAccessException, InvocationTargetException {
        String id = buffer.get("id");
        if (StringUtils.isBlank(id)){
            throw new BusinessException("参数为空！");
        }

        OaSpcyJcyFybxdtssxsq updateBean = OaSpcyJcyFybxdtssxsqService.getById(id);
        if (updateBean == null){
            throw new BusinessException("没有相匹配数据！");
        }

        Map<String, Object> oldMap = ObjectToMapUtils.objectToMap(updateBean);

        ConvertUtils.register(new DateConvert(), Date.class);
        // 自动完成页面到bean的数据对接
        BeanUtils.populate(updateBean, buffer);

        // 额外费用
        if (StringUtils.isNotBlank(updateBean.getFewfybxsqrid())){
            List<YcSysUser> userList = oaSysUserService.findRealnameListByIds(StringPatternUtils.getListByIds(updateBean.getFewfybxsqrid()));
            String sqr = "";
            if (CollectionUtil.isNotEmpty(userList)){
                for (YcSysUser user : userList){
                    if (StringUtils.isNotBlank(sqr)){
                        sqr += "," + user.getRealname();
                    }else {
                        sqr += user.getRealname();
                    }
                }
            }
            updateBean.setFewfybxsqrxm(sqr);
        }

        // 午休
        if (StringUtils.isNotBlank(updateBean.getFwxfsqrid())){
            List<YcSysUser> userList = oaSysUserService.findRealnameListByIds(StringPatternUtils.getListByIds(updateBean.getFwxfsqrid()));
            String sqr = "";
            if (CollectionUtil.isNotEmpty(userList)){
                for (YcSysUser user : userList){
                    if (StringUtils.isNotBlank(sqr)){
                        sqr += "," + user.getRealname();
                    }else {
                        sqr += user.getRealname();
                    }
                }
            }
            updateBean.setFwxfsqrxm(sqr);
        }

        updateBean.setUpdateTime(new Date());
        updateBean.setUpdateBy(GetCurrentUserUtils.getCurrentUserAccount());
        OaSpcyJcyFybxdtssxsqService.updateById(updateBean);

        String operContent = "基本信息修改：";
        operContent += LogUtils.BeanModifyMsg(updateBean,oldMap);

        spcyLogService.addLogs(LogConstant.LOGS_JCY_FYBXD,operContent,updateBean.getFbxdid(),"修改特殊事项申请", SpcyConstant.LOGS_SYS_TYPE_XG);
    }

    /**
     * 删除-特殊事项申请
     */
    public void deleteFybxdTssxSq(String id){
        if (StringUtils.isBlank(id)){
            throw new BusinessException("参数为空！");
        }

        OaSpcyJcyFybxdtssxsq bean = OaSpcyJcyFybxdtssxsqService.getById(id);
        if (bean == null){
            throw new BusinessException("没有相匹配的数据！");
        }
        OaSpcyJcyFybxdtssxsqService.removeById(id);

        String log = LogUtils.MapToStringMethod(ObjectToMapUtils.objectToMap(bean));
        spcyLogService.addLogs(LogConstant.LOGS_JCY_FYBXD,log,bean.getFbxdid(),"删除特殊事项申请", SpcyConstant.LOGS_SYS_TYPE_SC);
    }

    /**
     * 更新报销单-特殊事项申请
     */
    private void updateBxdTssxsq(String fbxdid){
        if (StringUtils.isBlank(fbxdid)){
            return;
        }

        LambdaUpdateWrapper<OaSpcyJcyFybxd> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(OaSpcyJcyFybxd::getId, fbxdid);

        OaSpcyJcyFybxd updateBean = new OaSpcyJcyFybxd();
        updateBean.setFsfytssxsq(OaSpcyJcyFybxdConstant.FYBXD_SFYTSSXSQ_YES);
        oaSpcyJcyFybxdService.update(updateBean, updateWrapper);

    }

    public Result addzzsq(NkRsZzsqd nkRsZzsqd){

//        BigDecimal allje = new BigDecimal(0);
//        if (StringUtils.isNotBlank(nkRsZzsqd.getFbxdid())){
//            List<NkRsZzsqd> nkRsZzsqds = nkRsZzzsqdService.list(new LambdaQueryWrapper<NkRsZzsqd>()
//                    .eq(NkRsZzsqd::getFskdw, nkRsZzsqd.getFskdw())
//                    .eq(NkRsZzsqd::getFbxdid, nkRsZzsqd.getFbxdid()));
//            if (CollectionUtils.isNotEmpty(nkRsZzsqds)){
//                //循环所有的数据 拿到金额 相加 虽然在这之后只有一个
//                for(NkRsZzsqd nkRsZzsqdss:nkRsZzsqds){
//                    String fjexx = nkRsZzsqdss.getFjexx();
//                    allje = allje.add(new BigDecimal(fjexx));
//                }
//                BigDecimal addje = allje.add(new BigDecimal(nkRsZzsqd.getFjexx()));
//                String id = nkRsZzsqds.get(0).getId();
//                NkRsZzsqd nkRsZzsqd1 = nkRsZzzsqdService.getById(id);
//                nkRsZzsqd1.setFjexx(String.valueOf(addje));
//                nkRsZzsqd1.setFjedx(NumberToChineseCapital.toChineseMoney(addje));
//            }
//
//        }


        NkRsZzsqd nkRsZzsqds = new NkRsZzsqd();
        nkRsZzsqds.setId(GetUuIdUtils.ReplaceUuId());
        nkRsZzsqds.setFbxdid(nkRsZzsqd.getFbxdid());
        nkRsZzsqds.setFsqsxid(nkRsZzsqd.getFsqsxid());

        nkRsZzsqds.setFskdw(nkRsZzsqd.getFskdw());
        nkRsZzsqds.setFzh(nkRsZzsqd.getFzh());
        nkRsZzsqds.setFkhyh(nkRsZzsqd.getFkhyh());
        nkRsZzsqds.setFjexx(nkRsZzsqd.getFjexx());
        nkRsZzsqds.setFjedx(NumberToChineseCapital.toChineseMoney(new BigDecimal(nkRsZzsqd.getFjexx())));


        nkRsZzsqds.setFwfqk(nkRsZzsqd.getFwfqk());
        nkRsZzsqds.setFfkfs(OaJcyFybxdConstant.FKFS_ZZ);

        nkRsZzsqds.setFbxdgrjlid(nkRsZzsqd.getFbxdgrjlid());
        nkRsZzsqds.setFsqrid(oaSpcyJcyFybxdgrjlService.getById(nkRsZzsqd.getFbxdgrjlid()).getFuserid());
        nkRsZzsqds.setFsqrxm(oaSpcyJcyFybxdgrjlService.getById(nkRsZzsqd.getFbxdgrjlid()).getRealname());
        nkRsZzsqds.setFjtxcdid(nkRsZzsqd.getFjtxcdid());
        nkRsZzzsqdService.save(nkRsZzsqds);

        return Result.ok("更新成功");
    }

    /**
     * 同步更新报销单人员的银行卡相关信息
     * */
    @Async(value = "asyncSimpleTask")
    public void syncBxdGrjlUserJbxx(String userid, YcSysUser user){
        OaSpcyJcyFybxdgrjl updateGrjl = new OaSpcyJcyFybxdgrjl();
        updateGrjl.setFyhkh(user.getFyhkh());
        updateGrjl.setFkfh(user.getFkfh());
        updateGrjl.setFkhhszd(user.getFkhhszd());

        LambdaUpdateWrapper<OaSpcyJcyFybxdgrjl> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(OaSpcyJcyFybxdgrjl::getFuserid, userid);
        updateWrapper.isNull(OaSpcyJcyFybxdgrjl::getFyhkh);

        oaSpcyJcyFybxdgrjlService.update(updateGrjl, updateWrapper);
    }

    /** 同步更新报销单人员的银行卡相关信息 */
    public HttpResult syncGrjlUserJbxx(String sqsxid, String id){
        List<OaSpcyJcyFybxdgrjl> list = null;

        if (StringUtils.isNotBlank(id)) {
            list = oaSpcyJcyFybxdgrjlService.list(new LambdaQueryWrapper<OaSpcyJcyFybxdgrjl>()
                    .eq(OaSpcyJcyFybxdgrjl::getId, id));
        }else {
            if (StringUtils.isBlank(sqsxid)){
                return HttpResult.error("参数为空！");
            }
            String bxdid = oaSpcyJcyFybxdMapper.selectIdBySqsxId(sqsxid);
            list = oaSpcyJcyFybxdgrjlService.list(new LambdaQueryWrapper<OaSpcyJcyFybxdgrjl>()
                    .eq(OaSpcyJcyFybxdgrjl::getFbxdid, bxdid));
        }

        if (CollectionUtil.isEmpty(list)){
            return HttpResult.error("没有相匹配的数据！");
        }

        for (OaSpcyJcyFybxdgrjl grjl : list){
            if (StringUtils.isBlank(grjl.getFuserid())){
                continue;
            }

            YcSysUser user = oaSysUserService.getById(grjl.getFuserid());
            if (user == null){
                continue;
            }

            grjl.setFkfh(user.getFkfh());
            grjl.setFyhkh(user.getFyhkh());
            grjl.setFkhhszd(user.getFkhhszd());
            grjl.setSex(user.getSex());
            grjl.setFgzdw(user.getFgzdw());
            oaSpcyJcyFybxdgrjlService.updateById(grjl);
        }

        return HttpResult.ok("同步信息成功");
    }















}
